package cn.jinjimi.app.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by zhenhua on 2018/2/6.
 */

public class HttpProtocolHelper {

    public static List<MultipartBody.Part> convert2Parts(Map<String, File> params) {

        if (params == null) {
            return null;
        }

        List<MultipartBody.Part> parts = new ArrayList<>();

        MediaType file = MediaType.parse("application/octet-stream");
        for (Map.Entry<String, File> entry : params.entrySet()) {
            if (entry.getValue() != null) {
                RequestBody rb = RequestBody.create(file, entry.getValue());
                MultipartBody.Part part = MultipartBody.Part.createFormData("file", (entry.getValue()).getName(), rb);
                parts.add(part);
            }
        }

        return parts;
    }
}
